package com.itheima.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.bos.constant.Constants;
import com.itheima.bos.domain.base.Area;
import com.itheima.bos.domain.take_delivery.Order;
import com.itheima.bos.web.action.common.BaseAction;
import com.itheima.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/order")
@Controller
@Scope("prototype")
public class OrderAction extends BaseAction<Order> {
	
	private String sendAreaInfo;
	private String recAreaInfo;
	
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value="order_save",results={@Result(name="success",type="redirect",location="../#/index")})
	public String order_save(){
		//发件人区域信息
		Area sendArea = new Area();
		String[] sendAreaArr = sendAreaInfo.split("/");
		sendArea.setProvince(sendAreaArr[0]);
		sendArea.setCity(sendAreaArr[1]);
		sendArea.setDistrict(sendAreaArr[2]);
		model.setSendArea(sendArea);
		//收件人区域信息
		Area recArea = new Area();
		String[] recAreaArr = recAreaInfo.split("/");
		recArea.setProvince(recAreaArr[0]);
		recArea.setCity(recAreaArr[1]);
		recArea.setDistrict(recAreaArr[2]);
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		if(customer != null){
			model.setCustomer_id(customer.getId());
			WebClient.create(Constants.BOS_MANAGEMENT_URL + "/services/orderServices/orderSave")
					.type(MediaType.APPLICATION_JSON)
					.post(model);
		}else{
			System.out.println("用户未登录!!!");
		}
		return SUCCESS;
	}
}
